package com.example.lib_cache.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.example.lib_cache.R;
import com.example.lib_cache.dao.OnOrderNvItemClickListener;
import com.example.lib_cache.databinding.ItemNvListBinding;
import com.example.lib_cache.model.OrderNavItem;

import java.util.List;

public class OrderNvAdapter extends RecyclerView.Adapter<OrderNvAdapter.OrderNvViewHolder>{

    private final static String TAG = OrderNavItem.class.getSimpleName();
    private List<OrderNavItem> list;

    public OrderNvAdapter(List<OrderNavItem> list){
        this.list = list;
    }

    public OrderNvAdapter() {

    }

    @NonNull
    @Override
    public OrderNvViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemNvListBinding itemNvListBinding = DataBindingUtil.inflate(
                LayoutInflater.from(parent.getContext()),
                R.layout.item_nv_list,
                parent,
                false);
        return new OrderNvViewHolder(itemNvListBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull OrderNvViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        holder.itemNvListBinding.setItem(list.get(position));
        holder.itemNvListBinding.executePendingBindings();

        holder.itemNvListBinding.tvStatusNvItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onOrderNvItemClickListener != null){
                    onOrderNvItemClickListener.showContextPerson(list.get(position));
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return null == list ? 0 : list.size();
    }

    public void updateData(List<OrderNavItem> orderNavItems) {
        if (null == orderNavItems) {
            return;
        }
        list = orderNavItems;
        notifyDataSetChanged();
    }

    private OnOrderNvItemClickListener onOrderNvItemClickListener;

    public void setOnOrderNvItemClickListener(OnOrderNvItemClickListener listener){
        onOrderNvItemClickListener = listener;
    }


    class OrderNvViewHolder extends RecyclerView.ViewHolder{
        ItemNvListBinding itemNvListBinding;

        public OrderNvViewHolder(ItemNvListBinding itemNvListBinding) {
            super(itemNvListBinding.getRoot());
            this.itemNvListBinding = itemNvListBinding;
        }
    }
}
